/* ExampleApplication.vala
 *
 * Copyright (C) 2008  Raphael Bosshard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Raphael Bosshard <raphael.bosshard@gmail.com>
 */

using GLib;
using Gtk;
using Appendage;
using ExampleApplication;

public class ExampleApplication.Application : GLib.Object {
	public PluginsEngine <ExampleApplication.IPlugin> plugins_engine ;
	public Core core;
	
	/**
	* Set up constants and paths
	*/
	public const string GCONF_KEY_ROOT = "/apps/exampleApplication/";
	public const string SYSTEM_PLUGINS_PATH = "plugins/";
	public const string USER_PLUGINS_SUBPATH = "userplugins/";
	
	
	construct {
		this.core = new Core ();
	}
	
	
	/**
	* Initialize Appendage (Plugin subsystem)
	*
	* Creates a new plugin engine, provides some plugin paths and tells the engine to load
	* all plugins found in there.
	* The plugins consist of a .plugin file and an object file. The .plugin file is read
	* and the appropriate object file is loaded.
	*/	
	public bool initialize_plugins_subsystem () {
	
		GLib.message ("Initializing plugin subsystem");
		this.plugins_engine = new Appendage.PluginsEngine <ExampleApplication.IPlugin> ();

		GLib.message ("Adding user plugins directory");			
		plugins_engine.add_plugins_directory (File.new_for_path (this.USER_PLUGINS_SUBPATH) );

		GLib.message ("Adding system home plugins directory");	
		plugins_engine.add_plugins_directory (File.new_for_path (this.SYSTEM_PLUGINS_PATH));
	
		/**
		* In the context of Appendage, loading means: read all the plugins directories, read all the
		* '.plugin' files and load the contained plugin information into a PluginInfo object
		**/
		GLib.message ("Loading plugins information");
		plugins_engine.load_all ();		
	
		/*
		* 'enable' will create an instance of each plugin. May be feched via the
		* the PluginInfo object
		*/
		GLib.message ("Enabling all plugin modules");
		plugins_engine.enable_all ();

		return true;
	}
	
	/**
	* Plugin activation: Get all plugin objects and do whatever there needs to be done. 
	* Activate and deactivate in these examples.
	*/
	public void activate_plugins () {
		GLib.debug ("Activating Plugins...");
		foreach (PluginInfo <ExampleApplication.IPlugin> info in plugins_engine.get_plugins_enabled ()) {
			GLib.debug ("Activating plugin '%s'", info.location);
			
			try {
				ExampleApplication.IPlugin pl = info.get_plugin_object ();
				pl.activate (this.core);	
				
			} catch (Appendage.ModuleLoaderError er) {
				GLib.warning ("Couldn't load plugin: %s", er.message);		
						
			} catch (Appendage.ModuleError er) {
				GLib.warning ("Couldn't load plugin: %s", er.message);
			}
		}
	} 
	
	public void deactivate_plugins () {
		foreach (PluginInfo <ExampleApplication.IPlugin> info in plugins_engine.get_plugins_enabled ()) {
			GLib.debug ("Deactivating plugin '%s'", info.location);
			
			try {
				ExampleApplication.IPlugin pl = info.get_plugin_object ();
				pl.deactivate ();	
				
			} catch (Appendage.ModuleLoaderError er) {
				GLib.warning ("Couldn't load plugin: %s", er.message);		
						
			} catch (Appendage.ModuleError er) {
				GLib.warning ("Couldn't load plugin: %s", er.message);
			}
		}
	}
	
	public void run () {
		 Gtk.main ();	
	}
	
	public void quit () {
		this.deactivate_plugins ();
		Gtk.main_quit ();
	}
}

/*
* Initializes 
*
*/ 
static void main (string[] args) {
	ExampleApplication.Application example;

	GLib.debug ("Starting example application");
	
	Gtk.init (ref args);
	
	example = new ExampleApplication.Application ();	
	example.initialize_plugins_subsystem ();
	example.activate_plugins ();

	example.run ();


	return;	
}



